<?php
/**
 * Created by PhpStorm.
 * User: mojie126
 * Date: 2017/9/7 0007
 * Time: 下午 16:58
 */

namespace app\backend\controller;

use app\common\svn;


class IndexController extends BackendBase
{
	public function index()
	{
		$this->assign("title", "后台首页");
		$this->assign("name", "系统信息");

		return $this->fetch();
	}

	public function svn($url, $preRev, $curRev)
	{
		$svn = new svn("mojie126", "a123456");
		$xml = simplexml_load_string($svn->getChangeLog($url, $preRev, $curRev));
		$tmp = [];
		$tt = json_decode(json_encode($xml), TRUE);
		foreach ($tt as $k) {
			foreach ($k as $key => $value) {
				$tmp['revision'] = $value['@attributes']['revision'];
				$tmp['author'] = $value['author'];
				if (count($value['paths']['path']) > 1) {
					foreach ($value['paths']['path'] as $kk) {
						$tmp['file'] = $kk;
						$data = [
							"url" => $url,
							"revision" => $tmp['revision'],
							"author" => $tmp['author'],
							"file" => $tmp['file']
						];
//						db("tmp")->insert($data);
					}
				}
			}
		}
	}

	/**
	 * @param array $array 二维数组，原始数据
	 * @param array $field 一维数组，[0]元素是用来分隔的，其他用来追加展示的
	 *
	 * @return array
	 */
	public function combine(array $array, array $field)
	{
		$tmpArray = array();
		$distinct_field = $field[0];
		unset($field[0]);
		foreach ($array as $row) {
			$key = $row[$distinct_field];
			if (array_key_exists($key, $tmpArray)) {
				foreach ($field as $value) {
					if (is_array($tmpArray[$key][$value])) {
						$tmpArray[$key][$value][] = $row[$value];
					} else {
						$tmpArray[$key][$value] = array($tmpArray[$key][$value], $row[$value]);
					}
				}
			} else {
				$tmpArray[$key][$distinct_field] = $row[$distinct_field];
				foreach ($field as $value) {
					$tmpArray[$key][$value] = array($row[$value]);
				}
			}
		}

		return $tmpArray;
	}

	public function get()
	{
		$this->assign("title", "TEST");
		$this->assign("name", "TEST");

		return $this->fetch();

	}

	public function getRes()
	{
		$data = input("param.");
		$info = db("tmp")->page($data['page'], $data['limit'])->select();
		$tmp = ["file", "revision", "author", "url"];
		$a1 = $this->combine($info, $tmp);
		foreach ($a1 as $k => $v) {
			if (is_array($v['revision'])) {
				$a1[$k]['revision'] = implode(",", $v['revision']);
			}
			if (is_array($v['author'])) {
				$a1[$k]['author'] = implode(",", $v['author']);
			}
			if (is_array($v['url'])) {
				$a1[$k]['url'] = implode(array_unique($v['url']));
			}
		}
		$this->jsonReturnLayUI(0, "", count($a1), $a1);
	}

	public function getDiff()
	{
		$info = input("param.");
		$url = $info['url'] . $info['file'];
		//判断是比较2个版本差异，还是比较和上一个版本的差异
		if (preg_match("/,/", $info['revision'])) {
			$tmp = explode(",", $info['revision']);
			$max = max($tmp);
			$min = min($tmp);
			$mode = 0;
		} else {
			$min = 0;
			$max = $info['revision'];
			$mode = 1;
		}
		$svn = new svn("mojie126", "a123456");
		$content = $svn->getDiffLog($url, $min, $max, $mode);
		//特殊的，防止HTML的注释被json_encode()清除
		if (empty($mode)) {
			jsonReturn(0, "", $content, 0, ["preRev" => $min, "curRev" => $max], TRUE);
		} else {
			jsonReturn(0, "", $content, 1, ["preRev" => $min, "curRev" => $max], TRUE);
		}
	}
}